package excel

import (
	"bytes"
	"fmt"
	"os"
	"strings"

	"golang.org/x/text/cases"
	"golang.org/x/text/language"
)

// ParseField 通用解析字段
func ParseField(cols [][]string) (map[int]string, map[string][]string) {
	res := make(map[int]string)
	resFieldType := make(map[string][]string)
	for colNum, col := range cols {

		if strings.HasPrefix(col[0], "#") {
			continue
		}
		if col[1] == "" || col[2] == "" {
			continue
		}
		if strings.Contains(col[1], "client") {
			continue
		}
		if !strings.Contains(col[1], "all") && !strings.Contains(col[1], "server") {
			continue
		}
		// fmt.Print(colNum, "\t")
		// 字段说明
		fieldExplain := strings.Replace(strings.Replace(col[0], "\n", "", -1), "\t", "", -1)
		// fmt.Print(fieldExplain, "\t")
		// 字段类型
		fieldType := col[1]
		if col[1] == "all" || col[1] == "server" {
			fieldType = "string"
		}
		// fmt.Print(fieldType, "\t")
		// 字段名字
		fieldName := Case2Camel(col[2])
		// fmt.Print(fieldName, "\t")

		// fmt.Println()
		res[colNum] = fieldName
		resFieldType[fieldName] = append(resFieldType[fieldName], fieldExplain, fieldType)

	}
	return res, resFieldType
}

// ParseValues 通用解析数据
func ParseValues(rows [][]string, index_field map[int]string, config_name, config_dir string) {
	buf := bytes.NewBufferString("[")
	var tmpRows [][]string
	// 去除空行 -- start
	for _, row := range rows {
		isContinue := false
		for _, ckv := range row {
			if ckv != "" {
				isContinue = true
				break
			}
		}
		if !isContinue {
			continue
		}
		tmpRows = append(tmpRows, row)
	}
	// 去除空行 -- end
	for rindex, row := range tmpRows {
		startStr := `{`
		buf.WriteString(startStr)
		allNum := len(index_field)
		tmpMap := make(map[string]struct{})
		for rnum, rv := range row {
			if fieldName, ok := index_field[rnum]; ok {
				allNum--
				if allNum == 0 { // 最后不加逗号
					buf.WriteString(`"` + fieldName + `":"` + rv + `"`)
				} else {
					buf.WriteString(`"` + fieldName + `":"` + rv + `",`)
				}
				tmpMap[fieldName] = struct{}{}
			}
		}
		if allNum > 0 {
			for _, k := range index_field {
				_, ok := tmpMap[k]
				if !ok {
					allNum--
					if allNum == 0 { // 最后不加逗号
						buf.WriteString(`"` + k + `":""`)
					} else {
						buf.WriteString(`"` + k + `":"",`)
					}
				}
			}

		}
		if rindex == len(tmpRows)-1 {
			endStr := `}`
			buf.WriteString(endStr)
		} else {
			endStr := `},` + "\n"
			buf.WriteString(endStr)
		}
	}
	buf.WriteString("]")
	if buf.String() == "[]" {
		return
	}
	os.WriteFile(config_dir+Case2Camel(config_name)+".json", buf.Bytes(), 0644)
	fmt.Println("=========解析文件名=========", config_name)
}

// ParseSpecialField 解析特殊配置文件字段
func ParseSpecialField(rows [][]string) map[string][]string {
	resFieldType := make(map[string][]string)
	for _, row := range rows[3:] {

		if row[3] == "client" {
			continue
		}

		// 字段说明
		fieldExplain := strings.Replace(strings.Replace(row[2], "\n", "", -1), "\t", "", -1)
		// fmt.Print(fieldExplain, "\t")
		// 字段类型
		fieldType := row[3]
		// fmt.Print(fieldType, "\t")
		// 字段名字
		fieldName := Case2Camel(row[0])
		// fmt.Print(fieldName, "\t")

		// fmt.Println()
		resFieldType[fieldName] = append(resFieldType[fieldName], fieldExplain, fieldType, row[1])

	}
	return resFieldType
}

// ParseSpecialValues 解析特殊配置文件数据
func ParseSpecialValues(allSpecFieldTypes map[string][]string, config_name, config_dir string) {
	buf := bytes.NewBufferString("{")
	allNum := len(allSpecFieldTypes)
	for fieldName, rv := range allSpecFieldTypes {

		allNum--
		if allNum == 0 { // 最后不加逗号
			buf.WriteString(`"` + fieldName + `":"` + rv[2] + `"`)
		} else {
			buf.WriteString(`"` + fieldName + `":"` + rv[2] + `",`)
		}

	}
	endStr := `}`
	buf.WriteString(endStr)
	if buf.String() == "{}" {
		return
	}
	os.WriteFile(config_dir+Case2Camel(config_name)+".json", buf.Bytes(), 0644)
	fmt.Println("=========解析文件名=========", config_name)
}

// Case2Camel 下划线写法转为驼峰写法
func Case2Camel(name string) string {
	name = strings.Replace(name, "_", " ", -1)
	cs := cases.Title(language.Tag{})
	name = cs.String(name)
	return strings.Replace(name, " ", "", -1)
}
